unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,StdCtrls,
  AcroMultiDDReader,AcroMultiDDWriter,AcroMultiConsts,
  AcroMultiDBDDreader,AcroMultiDBDDWriter,
  DB, ADODB;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    Button2: TButton;
    Label1: TLabel;
    OD: TOpenDialog;
    SD: TSaveDialog;
    Memo1: TMemo;
    Button3: TButton;
    ADODataSet1: TADODataSet;
    Button4: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
    procedure LoadDictionaryFromFile(AHandler:IProgressHandler;AParams:Variant);
    procedure WriteDictionaryToFile(AHandler:IProgressHandler;AParams:Variant);
    procedure WriteDictionaryToDB(AHandler:IProgressHandler;AParams:Variant);

    function _CreateDataReader(ADDFile:string):TCustomDDReader;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses FormProgress_U;

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  ADesciption,AFilter:string;
begin
  DDReaderWriterStorage.BuildFilterStrings(nil,ADesciption,AFilter);
  OD.Filter:=ADesciption;
  SD.Filter:=ADesciption;
  SD.DefaultExt:='.'+DDReaderWriterStorage.DDReaderWriterData[0]^.Ext;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  OD.FileName:='';
  if OD.Execute then
  begin
    Edit1.Text:=OD.FileName;
    FormProgress_U.StartProgress(LoadDictionaryFromFile,OD.FileName,true);
  end;
end;

procedure TForm1.LoadDictionaryFromFile(AHandler: IProgressHandler;
  AParams: Variant);
var
  IDR:IDataRead;
  ADDFile: string;
  i:integer;
begin
  ADDFile:=AParams;
  IDR:=_CreateDataReader(ADDFile);
  try
    Memo1.Clear;
    for i:=0 to IDR._GetFieldCount-1 do
    begin
      Memo1.Lines.Add(IDR._GetFieldName(i));

      AHandler.Progress((i+1)/IDR._GetFieldCount);
      if AHandler.IsCanceld then break;
    end;
    Memo1.Lines.Add('---RecordCount:'+InttoStr(IDR._GetRecordCount));
  finally
    IDR:=nil;
  end;
end;

function TForm1._CreateDataReader(ADDFile: string): TCustomDDReader;
var
  AExt:string;
  FM:TFileStream;
  AData:PDDReaderWriterData;
begin
  FM:=TFileStream.Create(ADDFile,fmOpenRead or fmShareDenyWrite);
  try
    AExt:=ExtractFileExt(ADDFile);
    Delete(AExt,1,1);
    AData:=DDReaderWriterStorage.GetDDReaderWriterDataByFileExt(AExt);
    if (AData=nil) or (AData^.ReaderClass=nil) then
      Raise Exception.Create(Format(S_FileDDReaderNotRegister,[AExt]));
    Result:=AData^.ReaderClass.Create(FM);
  finally
    FM.Free;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  SD.FileName:='';
  if SD.Execute then
  begin
    FormProgress_U.StartProgress(WriteDictionaryToFile,SD.FileName,true);
  end;
end;

procedure TForm1.WriteDictionaryToFile(AHandler: IProgressHandler;
  AParams: Variant);
var
  AData:PDDReaderWriterData;
  AStream:TFileStream;
  AWriter:TCustomStreamDDWriter;
  ADDFile,AExt:string;
  IDR:IDataRead;
begin
  ADDFile:=AParams;
  AExt:=ExtractFileExt(ADDFile);
  Delete(AExt,1,1);
  AData:=DDReaderWriterStorage.GetDDReaderWriterDataByFileExt(AExt);

  IDR:=_CreateDataReader(Edit1.Text);
  if (AData<>nil) and (AData^.WriterClass<>nil) then
    AWriter:=AData^.WriterClass.Create
  else AWriter:=TTextCSVStreamDDWriter.Create;
  AStream:=TFileStream.Create(ADDFile,fmCreate or fmOpenWrite);
  try
    AWriter.WriteSource.Reader:=IDR;
    AWriter.ProgressHandler:=AHandler;
    AWriter.DestinationStream:=AStream;
    AWriter.Write;
  finally
    AStream.Free;
    AWriter.Free;
    IDR:=nil;
  end;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  FormProgress_U.StartProgress(WriteDictionaryToDB,Integer(ADODataSet1),true);
end;

procedure TForm1.WriteDictionaryToDB(AHandler: IProgressHandler;
  AParams: Variant);
var
  ADBWriter:TDBDDWriter;
  IDR:IDataRead;
  ADataSet:TDataSet;
begin
  ADataSet:=TDataSet(integer(AParams));
  IDR:=_CreateDataReader(Edit1.Text);
  ADBWriter:=TDBDDWriter.Create;
  try
    ADBWriter.WriteSource.Reader:=IDR;
    ADBWriter.ProgressHandler:=AHandler;
    ADBWriter.DestinationTable:=ADataSet;
    ADBWriter.Write;
  finally
    ADBWriter.Free;
    IDR:=nil;
  end;
end;

procedure TForm1.Button4Click(Sender: TObject);
var
  IDR:IDataRead;
  i:integer;
begin
  IDR:=TDBDDReader.Create(ADODataSet1,nil);
  try
    Memo1.Clear;
    for i:=0 to IDR._GetFieldCount-1 do
    begin
      Memo1.Lines.Add(IDR._GetFieldName(i));

//      AHandler.Progress((i+1)/IDR._GetFieldCount);
//      if AHandler.IsCanceld then break;
    end;
    Memo1.Lines.Add('---RecordCount:'+InttoStr(IDR._GetRecordCount));
  finally
    IDR:=nil;
  end;
end;

end.
